<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;
use Think\Controller;
use Com\Wechat\Oauth;
use User\Api\UserApi;


/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	public $version = ''; 
  public function oauth(){
  	if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) { 
    
  	}
  	$oauth = new Oauth();
  	$oauth->init($this->get_url());
    //通过code获得openid
    if (!isset($_GET['code'])){     
      $url2 = $oauth->get_code_by_authorize(1);
      exit();
    } else {
      //获取code码，以获取openid
      $code = $_GET['code'];
      $user_info = $oauth->get_userinfo_by_authorize($code);
      if($user_info && $user_info['openid']){
        session('wechat_info',$user_info,3600);
        return;
      }
      else{
        E('Oauth 失败');
      }
    }
  }

  public function testOauth(){
    $session = $this->newoauth();
    
    var_dump($session);
  }

  private function get_url() {
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
  }

  //系统首页
  public function index(){
    // $this->oauth();
    // $user_info = session('wechat_info');
    // $re_visitor = M('visitor')->where(array('openid'=>$user_info['openid']))->find();
    // if($re_visitor){
      // session('visitor_id',$re_visitor['id'],3600);
    // }
    // else{
    //    $user = M('visitor')->add(array('openid'=>$user_info['openid'],'nickname'=>$user_info['nickname'],'reg_time'=>time()));
    //    session('visitor_id',$user,3600);
    // }
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/home");
  }

  //展览详情页
  public function exhibition(){
    $this->oauth();
    $exhibitionId = I('get.exhibitionId');
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/ticket_detail/".$exhibitionId);
    exit();
  }

  //全部作品
  public function artlist(){
    $this->oauth();
    $categoryId = I('get.categoryId');
    if($categoryId){
      redirect(__ROOT__."/Public/museum/www".$this->version."/#/artlist/".$categoryId);
    }else{
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/artlist/10");
    }
    exit();
  }

  //作品详情
  public function workDetail(){
    $this->oauth();
    $workId = I('get.id');
    if($workId){
      redirect(__ROOT__."/Public/museum/www".$this->version."/#/bigart/".$workId);
    }else{
      redirect(__ROOT__."/Public/museum/www".$this->version."/#/home");
    }
    exit();
  }

  //跳转到票务详情
  public function gotoTicketDetail(){
    $this->oauth();
    // $user_info = session('wechat_info');
    // $re_visitor = M('visitor')->where(array('openid'=>$user_info['openid']))->find();
    // if($re_visitor){
    //   session('visitor_id',$re_visitor['id'],3600);
    // }
    $id = I('get.id');
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/ticket_detail/".$id);
    exit();
  }

  /**
  *票券下单 post
  *@param: ticket_id,amount
  *@return:
  */
  public function confirmTicketOrder(){
    $post = json_decode(file_get_contents('php://input'), true);
    if(!$post['ticket_id']){
      $this->response(array('info'=>'参数错误'),json,400);
    }
    if(!$post['amount']||$post['amount']<=0){
      $this->response(array('info'=>'请选择至少一张票进行购买'),json,400);
    }

    $wechat_session = session('wechat_info');
    $uid = cookie('visitor_id');
    if(!$uid){
      return ;
    }
    if(!$wechat_session){
      return ;
    }
    $user = M('visitor')->find($uid);
    $re_visitor = M('visitor')->where(array('openid'=>$wechat_session['openid'],'phone'=>$user['phone']))->find();
    if(!$re_visitor){
      M('visitor')->where(array('id'=>$uid))->save(array('openid'=>$wechat_session['openid'],'nickname'=>$wechat_session['nickname']));
    }

    A('Home/Wechat')->confirmTicketOrder($uid,$post['ticket_id'],$post['amount']);
  }

  /**
  *个人信息
  *@param: 
  *@return:
  */
  public function gotoMyticket(){
    $uid = cookie('visitor_id');
    if(!$uid){
      redirect(__ROOT__."/Public/museum/www/#/login");
    }
    redirect(__ROOT__."/Public/museum/www/#/my_ticket");
  }

  /**
  *登录
  *@param: 
  *@return:
  */
  public function login(){
    $this->oauth();
    redirect(__ROOT__."/Public/museum/www/#/login");
  }

  //结算页
  public function checkpay(){
    $this->oauth();
    $orderId = I('get.orderId');
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/checkpay/".$orderId);
    exit();
  }

  //产品详情页
  public function productDetail(){
    $productId = I('get.productId');
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/productdetail/".$productId);
    exit();
  }

  //活动列表页
  public function activityListPage(){
    $extype = I('get.extype');
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/activitylist/".$extype);
    exit();
  }

  //vip结算页
  public function vip(){
    $this->oauth();
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/vip");
    exit();
  }

  //vip结算页
  public function vipcheckpay(){
    $this->oauth();
    $type = I('get.type');
    redirect(__ROOT__."/Public/museum/www".$this->version."/#/checkvip/".$type);
    exit();
  }

  public function showsession(){
    $session = session('wechat_info');
    var_dump($session);
  }

}